package com.tarena.lbs.user.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 手机小程序用户处理
 */
@Api(tags = "用户接口")
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户注册")
    //小程序新增注册
    @PostMapping("/basic/user/info/register")
    public Result<Void> register(@RequestBody UserParam param)
        throws BusinessException{
        String phone  = param.getPhone();
        Long num = userService.PhoneConfirm(phone);
        if(num > 0){
            return Result.fail(PassportResultEnum.USER_EXIST);
        }else {
            userService.register(param);
            return Result.success();
        }

    }
    //解析jwt 拿到id获取登录用户性情
    @ApiOperation(value = "用户详情")
    @GetMapping("/basic/user/detail")
    public Result<UserVO> detail()throws BusinessException{
        UserVO userVO = userService.detail();
        Result<UserVO> result = new Result<>(userVO);
        return result;
    }


    //用户提交绑定关联所选标签
    @ApiOperation(value = "用户标签关联")
    @GetMapping("/basic/user/tag")
    public Result<Void> bindUserTags(UserTagsParam param)
            throws BusinessException{
        userService.addTags(param);
        return Result.success();
    }

}
